clear
do "...\First.do"

********************************************************************************
* Years 2005-2019
********************************************************************************

use "$raw\gp_all_costs_gp.dta" 
drop if pnr==""

g help=substr(speciale,1,2)
*tab help

g GP=(help=="80")
keep if GP==1 

bys pnr year GP: egen total_cost_GP=total(bruhon) if GP==1
keep pnr year total_cost_GP 
duplicates drop

bys pnr year: g N=_N
gsort pnr year -total_cost_GP
replace total_cost_GP=total_cost_GP[_n-1] if total_cost_GP==. & pnr==pnr[_n-1] & year==year[_n-1] & N>1

drop N
duplicates drop

save "$work\costs1.dta", replace

********************************************************************************
* Years 1990-2005
********************************************************************************

clear
use "$raw\gp_all_costs_gp2.dta"
drop if pnr==""
drop if year==2005

g help=substr(speciale,1,2)
g GP=(help=="80")
keep if GP==1 

bys pnr year GP: egen total_cost_GP=total(bruhon) if GP==1

keep pnr year total_cost_GP 
duplicates drop

bys pnr year: g N=_N
gsort pnr year -total_cost_GP
replace total_cost_GP=total_cost_GP[_n-1] if total_cost_GP==. & pnr==pnr[_n-1] & year==year[_n-1] & N>1

keep pnr year total_cost_GP 
replace total_cost_GP=floor(total_cost_GP/100) // Until 2004 costs were recorded in Danish "øre" (1/100 kroner) 
duplicates drop

append using "$work\costs1.dta"

save "$work\costs2.dta", replace



